package com.htthorizon.powersupply.utils;



import java.util.ArrayList;
import java.util.List;


/**
 * @author Brilliant
 * *  用于服务市场分页的工具类
 */

public class PageUtil<T,N extends Number> {

    // 数据量总数
    private final N total;

    // 跨度
    private final N span;


    public PageUtil(N total, N span) {
        this.total = total;
        this.span = span;
    }


    public List<Page<T,N>> buildPage() {
        if (total instanceof Long){
            long spanL = span.longValue() ;
            long totalL =total.longValue() ;
            return buildPage(spanL,totalL);
        }
        if (total instanceof Integer){
            int spanI = span.intValue();
            int totalI = total.intValue();
            return buildPage(spanI,totalI);
        }
        throw new RuntimeException("分页仅支持 Long 和 Integer 类型");
    }

    private List<Page<T,N>> buildPage(long span, long total) {
        List<Page<T,N>> pages = new ArrayList<>();
        if (span >= total) {
            Page<T,N> objectPage = new Page(1L, span, total);
            pages.add(objectPage);
            return pages;
        }
        long size = total / span;
        if (total % span != 0) {
            for (long i = 1; i <= size + 1; i++) {
                Page<T,N> objectPage = new Page(i, span, total);
                pages.add(objectPage);
            }

        }else {
            for (long i = 1; i <= size; i++) {
                Page<T,N> objectPage = new Page(i, span, total);
                pages.add(objectPage);
            }
        }
        return pages;

    }

    private List<Page<T,N>> buildPage(int span, int total) {
        List<Page<T,N>> pages = new ArrayList<>();
        if (span >= total) {
            Page<T,N> objectPage = new Page(1, span, total);
            pages.add(objectPage);
            return pages;
        }
        long size = total / span;
        if (total % span != 0) {
            for (int i = 1; i <= size + 1; i++) {
                Page<T,N> objectPage = new Page(i, span, total);
                pages.add(objectPage);
            }
        }else {
            for (int i = 1; i <= size; i++) {
                Page<T,N> objectPage = new Page(i, span, total);
                pages.add(objectPage);
            }
        }
        return pages;
    }


    public static class Page<T,N extends Number> {
        private N pageNum;
        private N pageSize;
        private N total;
        private List<T> records;

        public Page(N pageNum, N pageSize, N total) {
            this.pageNum = pageNum;
            this.pageSize = pageSize;
            this.total = total;
        }

        public N getPageNum() {
            return pageNum;
        }

        public void setPageNum(N pageNum) {
            this.pageNum = pageNum;
        }

        public N getPageSize() {
            return pageSize;
        }

        public void setPageSize(N pageSize) {
            this.pageSize = pageSize;
        }

        public N getTotal() {
            return total;
        }

        public void setTotal(N total) {
            this.total = total;
        }

        public List<T> getRecords() {
            return records;
        }

        public void setRecords(List<T> records) {
            this.records = records;
        }
    }
}

